<template>
    <!-- <el-dialog v-model="isShow" size="50%" :destroy-on-close="true" @close="closeHandle"> -->
    <el-drawer v-model="isShow" :title="title" :destroy-on-close="true" size="50%" :close-on-press-escape="false"
        :close-on-click-modal="false" :show-close="false">
        <template #header="{ close, titleId, titleClass }">
            <h4 :id="titleId" :class="titleClass">{{ title }}</h4>
        </template>
        <el-form :model="form" label-width="auto" ref="formRef" :rules="rules">
            <el-row :gutter="20">
                <el-col :span="24">
                    <el-form-item label="所属单位" required prop="unitId">
                        <el-tree-select v-model="form.unitId" :data="deptOptions" default-expand-all
                            :props="{ value: 'deptId', label: 'deptName', children: 'children' }" value-key="deptId"
                            placeholder="选择上级部门" check-strictly
                            :disabled="isButtonDisabled" />
                    </el-form-item>
                </el-col>
                <el-col :span="24">
                    <el-form-item label="赃证物柜名称" required prop="roomcode">
                        <el-input v-model="form.roomcode" placeholder="请输入房间编号" />
                    </el-form-item>
                </el-col>
                <el-col :span="24">
                    <el-form-item label="赃证物柜编码" required prop="roomname">
                        <el-input v-model="form.roomname" placeholder="请输入房间名称" />
                    </el-form-item>
                </el-col>
                <el-col :span="24">
                    <el-form-item label="层号" required prop="roomname">
                        <el-input v-model="form.roomname" placeholder="请输入房间名称" />
                    </el-form-item>
                </el-col>
                <el-col :span="24">
                    <el-form-item label="格号" required prop="roomname">
                        <el-input v-model="form.roomname" placeholder="请输入房间名称" />
                    </el-form-item>
                </el-col>
                <el-col :span="24">
                    <el-form-item label="RFID主板号" required prop="roomname">
                        <el-input v-model="form.roomname" placeholder="请输入房间名称" />
                    </el-form-item>
                </el-col>
                <el-col :span="24">
                    <el-form-item label="RFID通道号" required prop="roomname">
                        <el-input v-model="form.roomname" placeholder="请输入房间名称" />
                    </el-form-item>
                </el-col>
                <el-col :span="24">
                    <el-form-item label="状态" required>
                        <el-radio-group v-model="form.state">
                            <el-radio v-for="(item, index) in sys_normal_disable" :key="'state' + index"
                                :value="item.value">{{
                                    item.label }}
                            </el-radio>
                        </el-radio-group>
                    </el-form-item>
                </el-col>
            </el-row>
        </el-form>

        <template #footer>
            <el-button type="primary" @click="submitHandle">提交</el-button>
            <el-button @click="isShow = false">关闭</el-button>
        </template>
    </el-drawer>
</template>

<script setup>
// import { createRoom, editRoom, getRoomInfo, getRoomTypeList } from "@/api/Room/room";
import { listDept, } from "@/api/system/dept";
import { getUserProfile } from "@/api/system/user";

const { proxy } = getCurrentInstance()
const { sys_normal_disable, nj_room_type } = proxy.useDict('sys_normal_disable', 'nj_room_type')
// 是否显示抽屉
const isShow = ref(false)
// 抽屉标题
const title = ref('')
// 部门
const department = ref([])
// 所属单位
const deptOptions = ref([]);
// 通知事件
const emit = defineEmits(['getRoomList'])

const rules = {
    roomcode: [{ required: true, message: "房间编号不能为空", trigger: "blur" }],
    roomname: [{ required: true, message: "房间名称不能为空", trigger: "blur" }],
    unitId: [{ required: true, message: "所属单位不能为空", trigger: "blur" }],
    policeareaId: [{ required: true, message: "所在警务区不能为空", trigger: "blur" }],
    roomtypeId: [{ required: true, message: "房间类别不能为空", trigger: "blur" }]
}

// 房间
const form = ref({
})

// 关闭后回调
const defaultQuery = {
}
const changeShowStatus = () => {
    isShow.value = !isShow.value
}
const isButtonDisabled = ref(false);

// 获取个人信息-所在单位
const getUserInfo = async () => {
    await getUserProfile().then(res => {
        form.value.unitId = res.data.unitId
    })
}
onMounted(() => {
    getDepartment()
})
// 新增
const showAddDrawer = () => {
    title.value = '新增档案柜信息';
    reset();
    getUserInfo()
    changeShowStatus()
}

// 修改房间 
const showEditDrawer = async (data,OperateType) => {
    title.value = '修改档案柜信息'
    if(OperateType == "edit"){
        title.value = '修改档案柜信息'
    }else{
        title.value = '档案柜信息详情'
    }
    // isButtonDisabled.value = true;
    // await getRoomInfo(data.roomId).then(res => {
    //     form.value = res.data
    //     form.value.unitId = Number(res.data.unitId)

        // if(form.value.unitId == ''){
        //     getUserInfo() 
        // }
    // })
    changeShowStatus()
}
defineExpose({ showAddDrawer, showEditDrawer })

// 表格属性
const Areapaging = ref({
    pageNum: 1,// 页码
    pageSize: 9999// 分页数量
})

//获取单位（部门）树
const getDepartment = async (data_deptId) => {
    if (data_deptId == "") {
        getUserInfo();              //找当前登录用户所在部门
        await listDept().then(res => {
        deptOptions.value = res.data.filter(
            item => item.isUnit === true
        );
        deptOptions.value = proxy.handleTree(deptOptions.value, "deptId");
    }) 
        // isButtonDisabled.value = false;
    } else {
        form.value.unitId = data_deptId
        // isButtonDisabled.value = true;
    }

    await listDept().then(res => {
        deptOptions.value = res.data.filter(
            item => item.isUnit === true
        );
        deptOptions.value = proxy.handleTree(deptOptions.value, "deptId");
    })
}



const submitHandle = () => {
    proxy.$refs["formRef"].validate(valid => {
        if (valid) {
            if (form.value['roomId']) {
                editRoom(form.value).then(res => {
                    changeShowStatus()
                    emit('getRoomList', '来了老弟')
                })
            } else {
                createRoom(form.value).then(res => {
                    changeShowStatus()
                    emit('getRoomList', '来了老弟')
                })
            }
        }

    });
}

// 关闭后的回调事件
const closeHandle = () => {
    form.value = defaultQuery
}
/** 重置操作表单 */
function reset() {
    form.value = {
        roomname: undefined,
        roomcode: undefined,
        roomtypeId: undefined,
        unitId: undefined,
        policeareaId: undefined,
        state: '0',
    };
};
/** 取消按钮 */
const cancel = () => {
    isShow.value = false
    // form.value = defaultQuery
    reset();
}

</script>

<style lang="scss" scoped>
h4 {
    margin: 10px 0;
}
</style>